package com.sxyh;

import java.io.File;

public class MavenClean {
    /**
     * FILE_NAME为Maven仓库的路径
     */
    public final static String FILE_PATH = "D:\\wwy\\git_lab\\athena\\maven_ebank";//C:\Users\Administrator\.m2\repository
    public final static String SUFFIX = ".lastUpdated";
    public final static String SUFFIX_REMOTE = "_remote.repositories";
    public final static String UNKNOWN = "unknown";
    public static int COUNT = 0;
    public static int SUM = 0;
    public static int SUM_BLANK = 0;

    public static void main(String[] args) {
        deleteErrorFile(new File(FILE_PATH));
        System.out.println("一共在Maven仓库中删除了");
        System.out.println(COUNT + "个错误文件");
        System.out.println(SUM + "个unknown文件夹");
        System.out.println(SUM_BLANK + "个空白文件夹");
    }

    public static void deleteErrorFile(File file) {
        if (file.isDirectory()) {
            if (file.getName().equals(UNKNOWN)) {
                if (file.delete()) {
                    SUM++;
                }
            } else {
                String[] list = file.list();
                if (list == null || list.length == 0) {
                    if (file.delete()) {
                        SUM_BLANK++;
                        deleteErrorFile(file.getParentFile());
                    }
                } else {
                    for (String childrenFile : list) {
                        deleteErrorFile(new File(file, childrenFile));
                    }
                }
            }
        } else {
            if (file.getName().endsWith(SUFFIX) || file.getName().endsWith(SUFFIX_REMOTE)) {
                if (file.delete()) {
                    COUNT++;
                    String[] list = file.getParentFile().list();
                    if (list == null || list.length == 0) {
                        deleteErrorFile(file.getParentFile());
                    }
                }
            }
        }
    }

}